﻿using UnityEngine;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor;
#endif
using System;

[Serializable]
public class BTAddNode : BTNodeBase
{

    public float nodeSum;
    public BTEditorNodeOutput output;
    public BTEditorNodeInput inputA;
    public BTEditorNodeInput inputB;
    public test testObj;

    #region Constrcutor
    public BTAddNode()
    {
        output = new BTEditorNodeOutput();
        inputA = new BTEditorNodeInput();
        inputB = new BTEditorNodeInput();
       
    }
    #endregion

    public override void InitNode()
    {
        base.InitNode();
       // nodeType = NodeType.Float;
        nodeRect = new Rect(10f, 10f, 200f, 65f);
        if(testObj == null)
        {
            testObj = ScriptableObject.CreateInstance<test>();
        }
    }

    public override void UpdateNode(Event e, Rect viewRect)
    {
        base.UpdateNode(e, viewRect);
    }

#if UNITY_EDITOR
    public override void UpdateNodeGUI(Event e, Rect viewRect, GUISkin viewSkin)
    {
        base.UpdateNodeGUI(e, viewRect, viewSkin);

        //output
        if (GUI.Button(new Rect(nodeRect.x + nodeRect.width, nodeRect.y + 5f, btnEditHeigh, btnEditHeigh),
                        new GUIContent(), viewSkin.GetStyle("NodeAddNormal")))
        {
            Debug.Log("clike ");
        }

        //inputA
        if (GUI.Button(new Rect(nodeRect.x - btnEditHeigh, nodeRect.y + 5f, btnEditHeigh, btnEditHeigh),
                        new GUIContent(), viewSkin.GetStyle("NodeAddNormal")))
        {
            if (parentGraph != null && parentGraph.connectionNode!= null)
            {
                inputA.inputNode = parentGraph.connectionNode;
                inputA.isOccupied = true;
                parentGraph.wantsConnection = false;
                parentGraph.connectionNode = null;
            }
        }

        //inputB
        if (GUI.Button(new Rect(nodeRect.x - btnEditHeigh, nodeRect.y + 10f + btnEditHeigh, btnEditHeigh, btnEditHeigh),
                        new GUIContent(), viewSkin.GetStyle("NodeAddNormal")))
        {
            if (parentGraph != null && parentGraph.connectionNode != null)
            {
                inputB.inputNode = parentGraph.connectionNode;
                inputB.isOccupied = true;
                parentGraph.wantsConnection = false;
                parentGraph.connectionNode = null;
            }
        }

        //画连接线
        if(inputA.isOccupied)
        {
            DrawInputLines();
        }
    }

    void DrawInputLines()
    {
        Handles.BeginGUI();
        Handles.color = Color.white;
        Handles.DrawLine(new Vector3(inputA.inputNode.nodeRect.x + inputA.inputNode.nodeRect.width + 24f,
                                     inputA.inputNode.nodeRect.y + inputA.inputNode.nodeRect.height *0.5f, 0f),
                         new Vector3(nodeRect.x - btnEditHeigh , nodeRect.y + 5f + 12f,0f));


        Handles.EndGUI();
    }

    public override void DrawNodeProperties()
    {
        base.DrawNodeProperties();
        if(testObj!= null)
        {
            Editor editor = Editor.CreateEditor(this.testObj);
            editor.OnInspectorGUI();
            
        }
    }
#endif
}
